/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bank_access;

import java.io.IOException;
import java.net.UnknownHostException;
import mware_lib.MiddlewareException;
import mware_lib.StringDecoder;
import mware_lib.Communicator.Client;

/**
 * @author Gortex
 */
//konkrete implementierung der abstrakten AccountImplBase klasse
public class AccountProxy extends AccountImplBase {
    //IP und Port des originalobjekts

    private String ipAdresse;
    private int port;
    private String reference;
    private Client client;

    protected AccountProxy(String ip, int port, String reference) {
        this.ipAdresse = ip;
        this.port = port;
        this.reference = reference;
//        System.out.println("AccountProxy gestartet mit ipAdresse=" + ipAdresse + " port" + port + " reference" + reference);
    }

    //Proxymethode die mthode zu einem String macht und diesen String an das skelleton auf der anderen Seite 
    //übergibt
    @Override
    public void transfer(double amount) throws OverdraftException {
        try {
            String message = "methodname:transfer;param1typ:double;param1value:" + amount;
            client = new Client(ipAdresse, port);
            client.send(reference + ";" + message);
            String reply = client.receive();

            if (!StringDecoder.hasException(reply)) {
                if (!StringDecoder.getReturnType(reply).equals("void")) {
                    client.close();
                    throw new MiddlewareException("AccountProxy: Return Type missmatch.");
                } else {
                    client.close();
                    return;
                }

            } else if (StringDecoder.getReturnType(reply).equals("OverdraftException")) {
                client.close();
                throw new OverdraftException(StringDecoder.getReturnValue(reply));

            } else {
                client.close();
                throw new MiddlewareException(StringDecoder.getReturnValue(reply));
            }

        } catch (UnknownHostException ex) {
            throw new MiddlewareException("AccountProxy: Skeleton nicht erreichbar Message: " + ex.getMessage());
        } catch (IOException ex) {
            throw new MiddlewareException("AccountProxy: Skeleton nicht erreichbar Message: " + ex.getMessage());
        }
    }

/**
     * Proxymethode die mthode zu einem String macht und diesen String
     * an das skelleton auf der anderen Seite übergibt
     * @return 
     */
    @Override
    public double getBalance() {
        try {
            String message = "methodname:getBalance;";
            client = new Client(ipAdresse, port);
            client.send(reference + ";" + message);
            String reply = client.receive();

            if (!StringDecoder.hasException(reply)) {

                if (!StringDecoder.getReturnType(reply).equals("double")) {
                    client.close();
                    throw new MiddlewareException("AccountProxy: Return Type missmatch.");

                } else {
                    client.close();
                    return Double.parseDouble(StringDecoder.getReturnValue(reply));
                }

            } else {
                client.close();
                throw new MiddlewareException(StringDecoder.getReturnValue(reply));
            }

        } catch (UnknownHostException ex) {
            throw new MiddlewareException("AccountProxy: Skeleton nicht erreichbar Message: " + ex.getMessage());
        } catch (IOException ex) {
            throw new MiddlewareException("AccountProxy: Skeleton nicht erreichbar Message: " + ex.getMessage());
        }
    }
}
